package kr.saeildamil.androidstudy.ch12;


import java.util.ArrayList;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDel extends Activity {
	ArrayList<String> item;
	ArrayAdapter<String> arAdapter;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
		
		item = new ArrayList<String>();
		item.add("나는 앱으로 억만장자가 되었다.");
		item.add("앱 제작 가이드북");
		item.add("안드로이드  프로그래밍 정복");
		
		arAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, item);
		
		list = (ListView) findViewById(R.id.ch12_listadddel_list);
		list.setAdapter(arAdapter);
		list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Toast.makeText(ListAddDel.this, item.get(position), Toast.LENGTH_SHORT).show();
				
			}
		});
	}
	
	public void mOnClick(View v){
		EditText et = (EditText) findViewById(R.id.ch12_listadddel_et);
		switch(v.getId()){
		case R.id.ch12_listadddel_addbtn:
			String text = et.getText().toString();
			if(et.length() != 0){
				item.add(text);
				et.setText("");
				arAdapter.notifyDataSetChanged();
			}
			break;
		case R.id.ch12_listadddel_delbtn:
			int pos = list.getCheckedItemPosition();
			if(pos != ListView.INVALID_POSITION){
				item.remove(pos);
				list.clearChoices();
				arAdapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
